package com.shequan.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ailk.dazzle.util.data.JSONUtils;
import com.ailk.dazzle.util.io.FileUtils;
import com.shequan.common.bean.FtpServerConfig;

/**
 * <P>
 * Copyright 2012 shequaner.
 * 文件上传下载controller
 * All Rights Reserved.
 * @author Liust
 * @version 1.0, 2012-3-1
 * @see com.shequan.common.controller.FileUpDownloadController 
 * @since 1.0
 */
@Controller
@RequestMapping("/file")
public class FileUpDownloadController {

	@RequestMapping(value = "/upload")
	public void upload(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String[] fileFullNames = FileUtils.uploadFileOnFTP(FtpServerConfig.getHostname(), FtpServerConfig.getPort(), FtpServerConfig.getUsername(), FtpServerConfig.getPassword(), req, FtpServerConfig.getBasePath(), "UTF-8");
		String json = JSONUtils.array2JsonString(fileFullNames);
		resp.getWriter().print(json);
	}
	
	@RequestMapping(value = "/download")
	public void download(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		FileUtils.downloadFileFromFTP(FtpServerConfig.getHostname(), FtpServerConfig.getPort(), FtpServerConfig.getUsername(), FtpServerConfig.getPassword(), resp, FtpServerConfig.getBasePath());
	}
	
}
